package com.fingard.dsp.bank.directbank.bos01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.io.FileHelper;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.io.BufferedReader;
import java.io.File;
import java.io.StringReader;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.Date;

public class BOS01BatPayRec extends BOS01Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        if ("198801".equals(p_req1988.reqHeader.commandCode)) {
            return d_198801_BatPay(p_req1988);
        } else {
            return d_1988_Foreign_BatPay(p_req1988);
        }
    }

    /**
     * 批量代付
     *
     * @param p_req1988
     * @return
     * @throws Exception
     */
    public RetAtsBatPayRec d_198801_BatPay(ReqBatPayRec p_req1988) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        retAts.reqSeqID = p_req1988.reqBatchNO;

        try {
            StringBuilder sbFileContent = new StringBuilder();

            String tmpPayDate = Format.Date8Format.format(p_req1988.getPayDateTime());
            String totalNum = p_req1988.totalNum;
            String totalAmt = p_req1988.totalAmt;
            String payAct = p_req1988.ownAct;

            String tmpReqHeader = MessageFormat.format("{0}|{1}|{2}|{3}||", tmpPayDate, totalNum, totalAmt, payAct);
            sbFileContent.append(tmpReqHeader).append("\r\n");

            while (p_req1988.readRecord()) {
                StringBuilder tmpReqBodyDetail = new StringBuilder();
                tmpReqBodyDetail.append(p_req1988.readedDetail.askNumber).append("|");
                tmpReqBodyDetail.append("").append("|");
                tmpReqBodyDetail.append("").append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.cnapsCode).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.cnapsCode).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.cnapsName).append("|");
                tmpReqBodyDetail.append(convertCertTypeToBank(p_req1988.readedDetail.certType)).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.certNum).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.recAct).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.recName).append("|");
                tmpReqBodyDetail.append("").append("|");

                String cardType = p_req1988.readedDetail.cardType;
                if ("1".equals(p_req1988.readedDetail.isPrivate)) {
                    if ("1".equals(cardType)) {
                        cardType = "1";
                    } else {
                        cardType = "2";
                    }
                } else {
                    cardType = "9";
                }
                tmpReqBodyDetail.append(cardType).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.payAmount).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.useType).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.memo).append("|");
                tmpReqBodyDetail.append("").append("|");
                tmpReqBodyDetail.append("").append("|");

                sbFileContent.append(tmpReqBodyDetail).append("\r\n");
            }
            WriteBankLogLn2("待上传批量代付请求文件内容:\r\n" + sbFileContent.toString());

            String tmpUploadFileName = tmpPayDate + "_" + p_req1988.reqBatchNO + ".txt";
            String tmpLocalMarkPath = getTempFileDirectory() + File.separator + "BatPay" + File.separator + tmpUploadFileName;
            WriteBankLogLn2("批次代付请求文件保存本机路径: " + tmpLocalMarkPath);
            FileHelper.writeFile(tmpLocalMarkPath, sbFileContent.toString(), getCharset());

            String[] tmpStrRet = uploadFile(tmpLocalMarkPath, p_req1988.reqBatchNO, "BatPay");
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");

                    String tmpRespCode = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    retAts.transState = tmpRespCode;
                    retAts.payInfoCode = tmpRespCode;

                    if ("0".equals(tmpRespCode)) {
                        String tmpBankBatNo = tmpBkReader.getFirstText("/BOSEBankData/opRep/opResult/BATCHNO");
                        WriteBankLogLn("银行返回批次号:" + tmpBankBatNo);
                        String tmpBatNoPath = getTempFileDirectory() + File.separator + "Mapping" + File.separator + p_req1988.reqBatchNO + ".txt";
                        WriteBankLogLn2("批次代付银行批次号保存本机路径: " + tmpBatNoPath);
                        FileHelper.writeFile(tmpBatNoPath, tmpBankBatNo, getCharset());
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    /**
     * 批量外币支付
     * <p>
     * 198810:境内批量代付
     * 198811:境外批量代付
     *
     * @param p_req1988
     * @return
     * @throws Exception
     */
    public RetAtsBatPayRec d_1988_Foreign_BatPay(ReqBatPayRec p_req1988) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req1988.reqHeader);
        retAts.reqSeqID = p_req1988.reqBatchNO;

        try {
            boolean jionforeignAct = getBankConfig().getValueAsBool("jionforeignAct");
            WriteBankLogLn("jionforeignAct:" + jionforeignAct);
            if (jionforeignAct) {
                String foreignActMapping = getBankConfig().getMapValueByKey("foreignActMapping", p_req1988.ownAct);
                WriteBankLogLn("foreignActMapping:" + "[key:" + p_req1988.ownAct + ",value:" + foreignActMapping + "]");
                p_req1988.ownAct = StringHelper.joinNotEmpty("", foreignActMapping, p_req1988.ownAct);
            }

            StringBuilder sbFileContent = new StringBuilder();

            StringBuilder seqNoMappingContent = new StringBuilder();

            int seqNo = 0;
            while (p_req1988.readRecord()) {
                StringBuilder tmpReqBodyDetail = new StringBuilder();
                tmpReqBodyDetail.append(++seqNo).append("|");
                tmpReqBodyDetail.append(convertCurToBank(p_req1988.readedDetail.recCur)).append("|");
                if ("3".equals(p_req1988.readedDetail.recCur)) {
                    retAts.respCode = "0";
                    retAts.respInfo = "不支持日币支付";
                    retAts.payInfoCode = FailCode.E9981;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.writeRetToATS();
                    return retAts;
                } else {
                    tmpReqBodyDetail.append(p_req1988.readedDetail.payAmount).append("|");
                }
                tmpReqBodyDetail.append("|");
                tmpReqBodyDetail.append(p_req1988.ownAct).append("/").append(convertCurToBank(p_req1988.readedDetail.payCur)).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.recBankLocation).append(" ").append(p_req1988.readedDetail.recBankAddress).append(" ").append(p_req1988.readedDetail.recAgentBankAccount).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.recName).append(" ").append(p_req1988.readedDetail.recNameAddress).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.recAct).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.feePayer).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.recCountry).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.payMethod).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.transCode).append("|");
                if ("3".equals(p_req1988.readedDetail.recCur)) {
                    retAts.respCode = "0";
                    retAts.respInfo = "不支持日币支付";
                    retAts.payInfoCode = FailCode.E9981;
                    retAts.transState = PaymentState.FAIL_Batch;
                    retAts.writeRetToATS();
                    return retAts;
                } else {
                    tmpReqBodyDetail.append(convertCurToBank(p_req1988.readedDetail.recCur)).append(p_req1988.readedDetail.payAmount).append("|");// 相应币种及金额
                }
                tmpReqBodyDetail.append(p_req1988.readedDetail.isBondedGoods).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.contractNo).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.invoiceNo).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.payProp).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.remittanceInfor).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.orgCode).append("|");
                tmpReqBodyDetail.append(p_req1988.readedDetail.transInfo).append("|");
                if (StringHelper.hasAnyChar(p_req1988.readedDetail.recAgtBankName) && StringHelper.hasAnyChar(p_req1988.readedDetail.recAgeBankAddr)) {
                    tmpReqBodyDetail.append(p_req1988.readedDetail.recAgtBankName).append(" ").append(p_req1988.readedDetail.recAgeBankAddr);
                } else {
                    tmpReqBodyDetail.append("");
                }

                seqNoMappingContent.append(p_req1988.readedDetail.askNumber).append(",").append(seqNo).append("\r\n");

                sbFileContent.append(tmpReqBodyDetail).append("\r\n");
            }
            WriteBankLogLn2("待上传批量代付请求文件内容:\r\n" + sbFileContent.toString());

            String foreignBusType = getBankConfig().getMapValueByKey("foreignBusType", p_req1988.ownAct);
            String tmpUploadFileName = bankFront.org + "_" + foreignBusType + "_" + p_req1988.reqBatchNO + ".txt";
            String tmpLocalMarkPath = getTempFileDirectory() + File.separator + "ForeignBatPay" + File.separator + tmpUploadFileName;
            WriteBankLogLn2("批次代付请求文件保存本机路径:\r\n " + tmpLocalMarkPath);
            FileHelper.writeFile(tmpLocalMarkPath, sbFileContent.toString(), getCharset());

            WriteBankLogLn2("本地外币支付流水映射文件内容:\r\n" + seqNoMappingContent.toString());
            String tmpSeqNoPath = getDataFileDirectory(p_req1988.getPayDateTime()) + File.separator + p_req1988.reqBatchNO + ".txt";
            WriteBankLogLn2("本地外币支付流水映射文件路径:" + tmpSeqNoPath);
            FileHelper.writeFile(tmpSeqNoPath, seqNoMappingContent.toString(), getCharset());

            String areaFlag = "";
            if ("198810".equals(p_req1988.reqHeader.commandCode)) {
                areaFlag = "0";
            } else {
                areaFlag = "1";
            }

            String[] tmpStrRet = uploadFile(tmpLocalMarkPath, p_req1988.reqBatchNO, "ForeignBatPay", areaFlag);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");

                    String tmpRespCode = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    retAts.transState = tmpRespCode;
                    retAts.payInfoCode = tmpRespCode;
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        if ("198801".equals(p_req1998.reqHeader.commandCode)) {
            return d_1998_198801_QryBatPay(p_req1998);
        } else {
            return d_1998_Foreign_QryBatPay(p_req1998);
        }
    }

    public RetAtsQryBatPayRec d_1998_198801_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
        retAts.reqSeqID = p_req1998.reqBatchNO;

        try {
            String[] tmpStrRet = downloadFile(p_req1998.reqBatchNO, p_req1998.reqBatchNO, "", "BatPay");
            if (tmpStrRet[0].length() == 0) {
                if (tmpStrRet[1].contains("<")) {
                    tmpStrRet[1] = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<"));
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                    retAts.writeAfterDetail();
                    return retAts;
                }

                String tmpRetStr = FileHelper.readFileText(tmpStrRet[1], getCharset());
                WriteBankLogLn2("本地批量代付结果文件内容:\r\n" + tmpRetStr);

                if (StringHelper.hasAnyChar(tmpRetStr)) {
                    retAts.respCode = "0";

                    StringReader strReader = new StringReader(tmpRetStr);
                    BufferedReader tmpReader = new BufferedReader(strReader);
                    tmpReader.readLine();

                    while (true) {
                        String eachline = tmpReader.readLine();
                        if (eachline == null) {
                            break;
                        }
                        if (eachline.indexOf("|") > 0) {
                            // 明细记录:0明细流水号|1收款行|2收款人开户行|3收款人账号|4收款人名称|5收款人地址|6交易金额|7备注|8用途|9备用字段1|10备用字段2|11备用字段3|12明细状态|13明细状态说明
                            String[] bodyValues = eachline.split("\\|");
                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                            tmpRetDetail.postScript = bodyValues[0];
                            tmpRetDetail.recAct = bodyValues[3];
                            tmpRetDetail.recName = bodyValues[4];
                            tmpRetDetail.payAmount = bodyValues[6];
                            tmpRetDetail.result = bodyValues[12];
                            tmpRetDetail.payInfoCode = bodyValues[12];
                            if (bodyValues.length > 13) {
                                tmpRetDetail.memo = bodyValues[13];
                            }

                            retAts.writeRetDetail(tmpRetDetail);
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = "银行返回明细数据格式非法!";
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "本地批次结果文件内容为空!";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeAfterDetail();
        return retAts;
    }

    public RetAtsQryBatPayRec d_1998_Foreign_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req1998.reqHeader);
        retAts.reqSeqID = p_req1998.reqBatchNO;

        try {
            String tmpSeqNoPath = getDataFileDirectory(p_req1998.getBgnDateTime()) + File.separator + p_req1998.reqBatchNO + ".txt";
            WriteBankLogLn2("本地外币支付流水映射文件路径:" + tmpSeqNoPath);
            String seqNoMappingContent = FileHelper.readFileText(tmpSeqNoPath, getCharset());
            WriteBankLogLn2("本地外币支付流水映射文件内容:\r\n" + seqNoMappingContent);

            if (StringHelper.isNullOrEmpty(seqNoMappingContent)) {
                retAts.respCode = "0";
                retAts.respInfo = "本地外币支付流水映射文件不存在,未发生交易";
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = "E9981";
                retAts.writeAfterDetail();
                return retAts;
            }
            String[] tmpSeqNoArray = seqNoMappingContent.split("\r\n");

            BOS01Header tmpBankHeader = new BOS01Header();
            tmpBankHeader.opName = "QryWHHKTransResult1_1OP";
            tmpBankHeader.serialNo = getQryTransID();
            tmpBankHeader.needSigned = false;
            tmpBankHeader.writeParam = false;

            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, tmpBankHeader);
            XElement tmpXElement = new XElement("ReqParam", "");
            tmpXElement.addChild(new XElement("BATCHNO", p_req1998.reqBatchNO));
            tmpXElement.addChild(new XElement("DETID", ""));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();

            WriteBankLogLn2("原始请求数据:\r\n" + xmlWriter.toXmlString());

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), tmpBankHeader);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retAts.respCode = "0";

                    String tmpRetCode = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    if ("0".equals(tmpRetCode)) {
                        while (tmpBkReader.read("/BOSEBankData/opRep/opResultSet/opResult")) {
                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                            String detID = tmpBkReader.readedXElement.getSingleText("DetID");
                            for (String seqNo : tmpSeqNoArray) {
                                if (StringHelper.isNullOrEmpty(seqNo)) {
                                    continue;
                                }

                                if (detID.equals(seqNo.split(",")[1])) {
                                    tmpRetDetail.postScript = seqNo.split(",")[0];
                                }
                            }

                            String payResult = tmpBkReader.readedXElement.getSingleText("DetStat");
                            if ("5".equals(payResult)) {
                                String payAmont = Format.DecimalAmtFormat.format(Double.parseDouble(tmpBkReader.readedXElement.getSingleText("CreditAmt")));
                                tmpRetDetail.payAmount = payAmont;
                                String exCur = tmpBkReader.readedXElement.getSingleText("DebitCurrency");
                                String oppCur = tmpBkReader.readedXElement.getSingleText("CreditCurrency");
                                tmpRetDetail.exCur = exCur;

                                String exchangeRate = tmpBkReader.readedXElement.getSingleText("ExchangeRate");
                                if (StringHelper.hasAnyChar(exchangeRate)) {
                                    BigDecimal tmpDec = new BigDecimal(exchangeRate);
                                    if (exCur.equalsIgnoreCase(oppCur)) {
                                        tmpRetDetail.exRate = "1";
                                    } else if ("JPY".equalsIgnoreCase(oppCur)) {
                                        tmpDec = tmpDec.divide(new BigDecimal("100000"));
                                        tmpRetDetail.exRate = String.valueOf(tmpDec);
                                        tmpRetDetail.exEquvAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpBkReader.readedXElement.getSingleText("DebitAmt")));
                                    } else {
                                        tmpDec = tmpDec.divide(new BigDecimal("100"));
                                        tmpRetDetail.exRate = String.valueOf(tmpDec);
                                        tmpRetDetail.exEquvAmt = Format.DecimalAmtFormat.format(Double.parseDouble(tmpBkReader.readedXElement.getSingleText("DebitAmt")));
                                    }
                                }
                            }

                            tmpRetDetail.result = "D+" + payResult;
                            tmpRetDetail.payInfoCode = "D+" + payResult;
                            tmpRetDetail.memo = tmpBkReader.readedXElement.getSingleText("DetStatMsg");

                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeAfterDetail();
        return retAts;
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_req9188.reqHeader);
        retAts.reqSeqID = p_req9188.reqBatchNO;

        try {
            StringBuilder seqNoMappingContent = new StringBuilder();

            StringBuilder sbFileContent = new StringBuilder();
            String totalNum = p_req9188.totalNum;
            String totalAmt = p_req9188.totalAmt;
            String batRecProtocolNo = getBankConfig().getValueAsString("batRecProtocolNo");
            if (StringHelper.isNullOrEmpty(batRecProtocolNo)) {
                WriteBankLogLn("缺失'批量代扣编号-batRecProtocolNo'配置节点!");
                retAts.respCode = "0";
                retAts.respInfo = "缺失'批量代扣编号-batRecProtocolNo'配置节点!";
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = "E9981";
                retAts.writeRetToATS();
                return retAts;
            }
            String tmpPayDate = Format.Date8Format.format(p_req9188.getPayDateTime());

            String tmpReqHeader = MessageFormat.format("{0}|{1}|{2}|{3}||", totalNum, totalAmt, batRecProtocolNo, tmpPayDate);
            sbFileContent.append(tmpReqHeader).append("\r\n");

            int index = 0;
            while (p_req9188.readRecord()) {
                index++;
                String payName = p_req9188.readedDetail.payName;
                String payAct = p_req9188.readedDetail.payAct;
                String payAmt = p_req9188.readedDetail.payAmount;
                String usage = p_req9188.readedDetail.useType;

                String tmpReqBodyDetail = MessageFormat.format("{0}|{1}|{2}|{3}|{4}", index, payName, payAct, payAmt, usage);
                sbFileContent.append(tmpReqBodyDetail).append("\r\n");

                seqNoMappingContent.append(p_req9188.readedDetail.askNumber).append(",").append(index).append("\r\n");
            }
            WriteBankLogLn2("待上传批次代扣请求文件内容:\r\n" + sbFileContent.toString());

            String tmpUploadFileName = bankFront.org + "_006_" + p_req9188.reqBatchNO + ".txt";
            String tmpLocalMarkPath = getTempFileDirectory() + File.separator + "BatRec" + File.separator + tmpUploadFileName;
            WriteBankLogLn2("批次代扣请求文件保存本机路径: " + tmpLocalMarkPath);
            FileHelper.writeFile(tmpLocalMarkPath, sbFileContent.toString(), getCharset());

            WriteBankLogLn2("本地批量代扣流水映射文件内容:\r\n" + seqNoMappingContent.toString());
            String tmpBatSeqNoPath = getDataFileDirectory(new Date()) + File.separator + p_req9188.reqBatchNO + ".txt";
            WriteBankLogLn2("本地批量代扣流水映射文件路径:" + tmpBatSeqNoPath);
            FileHelper.writeFile(tmpBatSeqNoPath, seqNoMappingContent.toString(), getCharset());

            String[] tmpStrRet = uploadFile(tmpLocalMarkPath, p_req9188.reqBatchNO, "BatRec");
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    retAts.respCode = "0";
                    retAts.respInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");

                    String tmpRespCode = tmpBkReader.getFirstText("/BOSEBankData/opRep/retCode");
                    retAts.transState = tmpRespCode;
                    retAts.payInfoCode = tmpRespCode;
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetToATS();
        return retAts;
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_req9198.reqHeader);
        retAts.reqSeqID = p_req9198.reqBatchNO;

        try {
            String tmpBatSeqNoPath = getDataFileDirectory(p_req9198.getBgnDateTime()) + File.separator + p_req9198.reqBatchNO + ".txt";
            WriteBankLogLn2("本地批量代扣流水映射文件路径:" + tmpBatSeqNoPath);
            if (!new File(tmpBatSeqNoPath).exists()) {
                retAts.respCode = "-1";
                retAts.respInfo = "本地批次映射文件不存在!";
                return retAts;
            }
            String seqNoMappingContent = FileHelper.readFileText(tmpBatSeqNoPath, getCharset());
            WriteBankLogLn2("本地批量代扣流水映射文件内容:\r\n" + seqNoMappingContent.toString());
            String[] seqNoArray = seqNoMappingContent.split("\r\n");

            String[] tmpStrRet = downloadFile(p_req9198.reqBatchNO, p_req9198.reqBatchNO, "006", "BatRec");
            if (tmpStrRet[0].length() == 0) {
                if (tmpStrRet[1].contains("<")) {
                    tmpStrRet[1] = tmpStrRet[1].substring(tmpStrRet[1].indexOf("<"));
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpBkReader.getFirstText("/BOSEBankData/opRep/errMsg");
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                    retAts.writeAfterDetail();
                    return retAts;
                }

                String tmpRetStr = FileHelper.readFileText(tmpStrRet[1], getCharset());
                WriteBankLogLn2("本地批量代扣结果文件内容:\r\n" + tmpRetStr);

                if (StringHelper.hasAnyChar(tmpRetStr)) {
                    retAts.respCode = "0";

                    StringReader strReader = new StringReader(tmpRetStr);
                    BufferedReader tmpReader = new BufferedReader(strReader);
                    tmpReader.readLine();

                    while (true) {
                        String eachline = tmpReader.readLine();
                        if (eachline == null) {
                            break;
                        }
                        if (eachline.indexOf("|") > 0) {
                            // 明细记录:序号0|户名1|卡号2|金额3|用途4|状态5|失败原因6
                            String[] bodyValues = eachline.split("\\|");
                            for (String tmpSeqNo : seqNoArray) {
                                if (StringHelper.isNullOrEmpty(tmpSeqNo)) {
                                    continue;
                                }
                                if ((tmpSeqNo.split(",")[1]).equals(bodyValues[0])) {
                                    RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();

                                    tmpRetDetail.payAct = bodyValues[2];
                                    tmpRetDetail.payName = bodyValues[1];
                                    tmpRetDetail.postScript = tmpSeqNo.split(",")[0];
                                    tmpRetDetail.payAmount = bodyValues[3];
                                    if (bodyValues.length > 6) {
                                        tmpRetDetail.memo = bodyValues[6];
                                    }
                                    tmpRetDetail.result = bodyValues[5];
                                    tmpRetDetail.payInfoCode = bodyValues[5];

                                    retAts.writeRetDetail(tmpRetDetail);
                                }
                            }
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = "银行返回明细数据格式非法!";
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "本地批次结果文件内容为空!";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeAfterDetail();
        return retAts;
    }
}
